package com.tuo51.manager.controller;


import com.tuo51.evaluate.service.article.ArticleService;
import com.tuo51.evaluate.vo.news.ArticleListItemVO;
import com.tuo51.evaluate.vo.news.ArticleVO;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.query.base.PagerQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/mapi/news")
@Api(value = "/mapi/news", tags = "OP - 新闻文章管理")
public class MAPINewsController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    MemVerificationCache cache;

    @RequestMapping(value = "/items", method = GET)
    @ApiOperation("获取所有新闻条目，含运营位等")
    public StatusResponse newsItemList(@RequestHeader(TICKET) String ticket,
                                       String channel,
                                       String category,
                                       String staticCode,
                                       String keyword,
                                       PagerQuery pagerQuery){
        if (pagerQuery.getPageSize() <= 0){
            pagerQuery.setPageSize(10);
        }

        String staffId = cache.getStaffId(ticket);
        if (StringUtils.isEmpty(channel)){
            channel = "__ALL__"; //hot fix for
        }

        return StatusResponse.success(articleService.newsList(true, channel, category, staticCode, keyword, pagerQuery));
    }

    @RequestMapping(value = "/edit_item", method = POST)
    @ApiOperation("编辑或者创建条目")
    public StatusResponse editNewsItem(@RequestHeader(TICKET) String ticket,
                                       ArticleListItemVO item){
        String staffId = cache.getStaffId(ticket);

        return StatusResponse.success(articleService.editItem(item));
    }

    @RequestMapping(value = "/articles", method = GET)
    @ApiOperation("文章列表")
    public StatusResponse login(@RequestHeader(TICKET) String ticket,
                                String keyword, PagerQuery pagerQuery){
        String staffId = cache.getStaffId(ticket);

        return StatusResponse.success(articleService.articleList(keyword, pagerQuery));
    }

    @RequestMapping(value = "/edit_article", method = POST)
    @ApiOperation("编辑或者创建文章")
    public StatusResponse login(@RequestHeader(TICKET) String ticket,
                                ArticleVO article){

        String staffId = cache.getStaffId(ticket);
        return StatusResponse.success(articleService.editArticle(article));
    }

    @RequestMapping(value = "/delete_article", method = POST)
    @ApiOperation("删除")
    public StatusResponse deleteArticle(@RequestHeader(TICKET) String ticket, String articleId) {
        String staffId = cache.getStaffId(ticket);

        return StatusResponse.success(articleService.deleteArticle(articleId));
    }


    @RequestMapping(value = "/delete", method = POST)
    @ApiOperation("删除")
    public StatusResponse deleteItem(@RequestHeader(TICKET) String ticket, String itemId) {
        String staffId = cache.getStaffId(ticket);

        return StatusResponse.success(articleService.deleteItem(itemId));
    }

}
